package com.tnovoselec.android.ui;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
 * The workspace is a wide area with a finite number of screens. A workspace is
 * meant to be used with a fixed width only.
 */
public class PageLayout extends ViewGroup {

    public static interface OnPageListener {
	void onPageChanged(PageLayout pageLayout, int page);

	void onScroll(PageLayout pageLayout, int scroll);
    }

    private static final int INVALID_SCREEN = -1;

    /**
     * The velocity at which a fling gesture will cause us to snap to the next
     * screen
     */
    private static final int SNAP_VELOCITY = 600;

    private int mDefaultScreen;

    private int mCurrentScreen;
    private int mNextScreen = INVALID_SCREEN;
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;

    private float mLastMotionX;
    private float mLastMotionY;

    private final static int TOUCH_STATE_REST = 0;
    private final static int TOUCH_STATE_SCROLLING = 1;

    private int mTouchState = TOUCH_STATE_REST;

    private boolean mAllowLongPress = true;

    private int mTouchSlop;
    private int mMaximumVelocity;

    private static final int INVALID_POINTER = -1;

    private int mActivePointerId = INVALID_POINTER;

    private static final float NANOTIME_DIV = 1000000000.0f;
    private static final float SMOOTHING_SPEED = 0.75f;
    private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED));
    private float mSmoothingTime;
    private float mTouchX;

    private WorkspaceOvershootInterpolator mScrollInterpolator;

    private Set<OnPageListener> mListeners;

    private static final float BASELINE_FLING_VELOCITY = 2500.f;
    private static final float FLING_VELOCITY_INFLUENCE = 0.4f;

    private static class WorkspaceOvershootInterpolator implements Interpolator {
	private static final float DEFAULT_TENSION = 0.7f;
	private float mTension;

	public WorkspaceOvershootInterpolator() {
	    mTension = DEFAULT_TENSION;
	}

	public void setDistance(int distance) {
	    mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
	}

	public void disableSettle() {
	    mTension = 0.f;
	}

	public float getInterpolation(float t) {
	    // _o(t) = t * t * ((tension + 1) * t + tension)
	    // o(t) = _o(t - 1) + 1
	    t -= 1.0f;
	    return t * t * ((mTension + 1) * t + mTension) + 1.0f;
	}
    }

    /**
     * Used to inflate the Workspace from XML.
     * 
     * @param context
     *            The application's context.
     * @param attrs
     *            The attribtues set containing the Workspace's customization
     *            values.
     */
    public PageLayout(Context context, AttributeSet attrs) {
	this(context, attrs, 0);
    }

    /**
     * Used to inflate the Workspace from XML.
     * 
     * @param context
     *            The application's context.
     * @param attrs
     *            The attribtues set containing the Workspace's customization
     *            values.
     * @param defStyle
     *            Unused.
     */
    public PageLayout(Context context, AttributeSet attrs, int defStyle) {
	super(context, attrs, defStyle);

	TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PageLayout, defStyle, 0);
	mDefaultScreen = a.getInt(R.styleable.PageLayout_defaultPage, 0);
	a.recycle();

	setHapticFeedbackEnabled(false);
	initWorkspace();
    }

    /**
     * Initializes various states for this workspace.
     */
    private void initWorkspace() {
	Context context = getContext();
	mListeners = new HashSet<OnPageListener>();
	mScrollInterpolator = new WorkspaceOvershootInterpolator();
	mScroller = new Scroller(context, mScrollInterpolator);
	mCurrentScreen = mDefaultScreen;

	final ViewConfiguration configuration = ViewConfiguration.get(getContext());
	mTouchSlop = configuration.getScaledTouchSlop();
	mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    }

    public boolean addOnPageListener(OnPageListener listener) {
	return mListeners.add(listener);
    }

    public boolean removeOnPageListener(OnPageListener listener) {
	return mListeners.remove(listener);
    }

    /**
     * Set a new distance that a touch can wander before we think the user is
     * scrolling in pixels slop<br/>
     * 
     * @param touchSlopP
     */
    public void setTouchSlop(int touchSlopP) {
	mTouchSlop = touchSlopP;
    }

    boolean isDefaultScreenShowing() {
	return mCurrentScreen == mDefaultScreen;
    }

    /**
     * Returns the index of the currently displayed screen.
     * 
     * @return The index of the currently displayed screen.
     */
    public int getCurrentScreen() {
	return mCurrentScreen;
    }

    /**
     * Sets the current screen.
     * 
     * @param currentScreen
     */
    public void setCurrentScreen(int currentScreen) {
	setCurrentScreen(currentScreen, false);
    }

    /**
     * Sets the current screen.
     * 
     * @param currentScreen
     */
    public void setCurrentScreen(int currentScreen, boolean animate) {
	if (!mScroller.isFinished())
	    mScroller.abortAnimation();
	mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
	if (animate) {
	    snapToScreen(mCurrentScreen);
	} else {
	    scrollTo(mCurrentScreen * getWidth(), 0);
	}
	invalidate();
    }

    /**
     * Registers the specified listener on each screen contained in this
     * workspace.
     * 
     * @param l
     *            The listener used to respond to long clicks.
     */
    @Override
    public void setOnLongClickListener(OnLongClickListener l) {
	final int count = getChildCount();
	for (int i = 0; i < count; i++) {
	    getChildAt(i).setOnLongClickListener(l);
	}
    }

    @Override
    public void scrollTo(int x, int y) {
	super.scrollTo(x, y);
	mTouchX = x;
	mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
	super.onScrollChanged(l, t, oldl, oldt);
	notifyListenersScroll(getScrollX());
    }

    @Override
    public void computeScroll() {
	int mScrollX = getScrollX();
	int mScrollY = getScrollY();
	if (mScroller.computeScrollOffset()) {
	    mTouchX = mScrollX = mScroller.getCurrX();
	    mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
	    mScrollY = mScroller.getCurrY();
	    scrollTo(mScrollX, mScrollY);
	    postInvalidate();
	} else if (mNextScreen != INVALID_SCREEN) {
	    mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
	    mNextScreen = INVALID_SCREEN;
	    clearChildrenCache();
	    if (mTouchState != TOUCH_STATE_SCROLLING) {
		notifyListenersChanged();
	    }
	} else if (mTouchState == TOUCH_STATE_SCROLLING) {
	    final float now = System.nanoTime() / NANOTIME_DIV;
	    final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
	    final float dx = mTouchX - mScrollX;
	    mScrollX += dx * e;
	    mSmoothingTime = now;
	    // Keep generating points as long as we're more than 1px away from
	    // the target
	    if (dx > 1.f || dx < -1.f) {
		postInvalidate();
	    }
	    scrollTo(mScrollX, mScrollY);
	}
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
	boolean restore = false;
	int restoreCount = 0;

	// ViewGroup.dispatchDraw() supports many features we don't need:
	// clip to padding, layout animation, animation listener, disappearing
	// children, etc. The following implementation attempts to fast-track
	// the drawing dispatch by drawing only what we know needs to be drawn.

	boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN;
	// If we are not scrolling or flinging, draw only the current screen
	if (fastDraw) {
	    drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime());
	} else {
	    final long drawingTime = getDrawingTime();
	    final float scrollPos = (float) getScrollX() / getWidth();
	    final int leftScreen = (int) scrollPos;
	    final int rightScreen = leftScreen + 1;
	    if (leftScreen >= 0) {
		drawChild(canvas, getChildAt(leftScreen), drawingTime);
	    }
	    if (scrollPos != leftScreen && rightScreen < getChildCount()) {
		drawChild(canvas, getChildAt(rightScreen), drawingTime);
	    }
	}

	if (restore) {
	    canvas.restoreToCount(restoreCount);
	}
    }

    @Override
    protected void onAttachedToWindow() {
	super.onAttachedToWindow();
	computeScroll();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	final int width = MeasureSpec.getSize(widthMeasureSpec);
	final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	if (widthMode != MeasureSpec.EXACTLY) {
	    throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
	}

	final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	if (heightMode != MeasureSpec.EXACTLY) {
	    throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
	}

	// The children are given the same width and height as the workspace
	final int count = getChildCount();
	for (int i = 0; i < count; i++) {
	    getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
	}

	setHorizontalScrollBarEnabled(false);
	scrollTo(mCurrentScreen * width, 0);
	setHorizontalScrollBarEnabled(true);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
	int childLeft = 0;

	final int count = getChildCount();
	for (int i = 0; i < count; i++) {
	    final View child = getChildAt(i);
	    if (child.getVisibility() != View.GONE) {
		final int childWidth = child.getMeasuredWidth();
		child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
		childLeft += childWidth;
	    }
	}
    }

    @Override
    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
	int screen = indexOfChild(child);
	if (screen != mCurrentScreen || !mScroller.isFinished()) {
	    snapToScreen(screen);
	    return true;
	}
	return false;
    }

    @Override
    public boolean dispatchUnhandledMove(View focused, int direction) {
	if (direction == View.FOCUS_LEFT) {
	    if (getCurrentScreen() > 0) {
		snapToScreen(getCurrentScreen() - 1);
		return true;
	    }
	} else if (direction == View.FOCUS_RIGHT) {
	    if (getCurrentScreen() < getChildCount() - 1) {
		snapToScreen(getCurrentScreen() + 1);
		return true;
	    }
	}
	return super.dispatchUnhandledMove(focused, direction);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

	/*
	 * This method JUST determines whether we want to intercept the motion.
	 * If we return true, onTouchEvent will be called and we do the actual
	 * scrolling there.
	 */

	/*
	 * Shortcut the most recurring case: the user is in the dragging state
	 * and he is moving his finger. We want to intercept this motion.
	 */
	final int action = ev.getAction();
	if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
	    return true;
	}

	acquireVelocityTrackerAndAddMovement(ev);

	switch (action & MotionEvent.ACTION_MASK) {
	    case MotionEvent.ACTION_MOVE: {
		/*
		 * mIsBeingDragged == false, otherwise the shortcut would have
		 * caught it. Check whether the user has moved far enough from
		 * his original down touch.
		 */

		/*
		 * Locally do absolute value. mLastMotionX is set to the y value
		 * of the down event.
		 */
		final int pointerIndex = MotionEventWrapper.findPointerIndex(ev, mActivePointerId);
		final float x = MotionEventWrapper.getX(ev, pointerIndex);
		final float y = MotionEventWrapper.getY(ev, pointerIndex);
		final int xDiff = (int) Math.abs(x - mLastMotionX);
		final int yDiff = (int) Math.abs(y - mLastMotionY);

		final int touchSlop = mTouchSlop;
		boolean xMoved = xDiff > touchSlop;
		boolean yMoved = yDiff > touchSlop;

		if (xMoved || yMoved) {

		    if (xMoved) {
			// Scroll if the user moved far enough along the X axis
			mTouchState = TOUCH_STATE_SCROLLING;
			mLastMotionX = x;
			mTouchX = getScrollX();
			mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
			enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
		    }
		    // Either way, cancel any pending longpress
		    if (mAllowLongPress) {
			mAllowLongPress = false;
			// Try canceling the long press. It could also have been
			// scheduled
			// by a distant descendant, so use the mAllowLongPress
			// flag to block
			// everything
			final View currentScreen = getChildAt(mCurrentScreen);
			currentScreen.cancelLongPress();
		    }
		}
		break;
	    }

	    case MotionEvent.ACTION_DOWN: {
		final float x = ev.getX();
		final float y = ev.getY();
		// Remember location of down touch
		mLastMotionX = x;
		mLastMotionY = y;
		mActivePointerId = MotionEventWrapper.getPointerId(ev, 0);
		mAllowLongPress = true;

		/*
		 * If being flinged and user touches the screen, initiate drag;
		 * otherwise don't. mScroller.isFinished should be false when
		 * being flinged.
		 */
		mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
		break;
	    }

	    case MotionEvent.ACTION_CANCEL:
	    case MotionEvent.ACTION_UP:

		// Release the drag
		clearChildrenCache();
		mTouchState = TOUCH_STATE_REST;
		mActivePointerId = INVALID_POINTER;
		mAllowLongPress = false;
		releaseVelocityTracker();
		break;

	    case MotionEvent.ACTION_POINTER_UP:
		onSecondaryPointerUp(ev);
		break;
	}

	/*
	 * The only time we want to intercept motion events is if we are in the
	 * drag mode.
	 */
	return mTouchState != TOUCH_STATE_REST;
    }

    private void onSecondaryPointerUp(MotionEvent ev) {
	final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
	final int pointerId = MotionEventWrapper.getPointerId(ev, pointerIndex);
	if (pointerId == mActivePointerId) {
	    // This was our active pointer going up. Choose a new
	    // active pointer and adjust accordingly.
	    // TODO: Make this decision more intelligent.
	    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
	    mLastMotionX = MotionEventWrapper.getX(ev, newPointerIndex);
	    mLastMotionY = MotionEventWrapper.getY(ev, newPointerIndex);
	    mActivePointerId = MotionEventWrapper.getPointerId(ev, newPointerIndex);
	    if (mVelocityTracker != null) {
		mVelocityTracker.clear();
	    }
	}
    }

    /**
     * If one of our descendant views decides that it could be focused now, only
     * pass that along if it's on the current screen.
     * 
     * This happens when live folders requery, and if they're off screen, they
     * end up calling requestFocus, which pulls it on screen.
     */
    @Override
    public void focusableViewAvailable(View focused) {
	View current = getChildAt(mCurrentScreen);
	View v = focused;
	while (true) {
	    if (v == current) {
		super.focusableViewAvailable(focused);
		return;
	    }
	    if (v == this) {
		return;
	    }
	    ViewParent parent = v.getParent();
	    if (parent instanceof View) {
		v = (View) v.getParent();
	    } else {
		return;
	    }
	}
    }

    void enableChildrenCache(int fromScreen, int toScreen) {
	if (fromScreen > toScreen) {
	    final int temp = fromScreen;
	    fromScreen = toScreen;
	    toScreen = temp;
	}

	final int count = getChildCount();

	fromScreen = Math.max(fromScreen, 0);
	toScreen = Math.min(toScreen, count - 1);

	for (int i = fromScreen; i <= toScreen; i++) {
	    final View layout = getChildAt(i);
	    layout.setDrawingCacheEnabled(true);
	}
    }

    void clearChildrenCache() {
	final int count = getChildCount();
	for (int i = 0; i < count; i++) {
	    final View layout = getChildAt(i);
	    layout.setDrawingCacheEnabled(false);
	}
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
	int mScrollX = getScrollX();
	acquireVelocityTrackerAndAddMovement(ev);

	final int action = ev.getAction();

	switch (action & MotionEvent.ACTION_MASK) {
	    case MotionEvent.ACTION_DOWN:
		/*
		 * If being flinged and user touches, stop the fling. isFinished
		 * will be false if being flinged.
		 */
		if (!mScroller.isFinished()) {
		    mScroller.abortAnimation();
		}

		// Remember where the motion event started
		mLastMotionX = ev.getX();
		mActivePointerId = MotionEventWrapper.getPointerId(ev, 0);
		if (mTouchState == TOUCH_STATE_SCROLLING) {
		    enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
		}
		break;
	    case MotionEvent.ACTION_MOVE:
		if (mTouchState == TOUCH_STATE_SCROLLING) {
		    // Scroll to follow the motion event
		    final int pointerIndex = MotionEventWrapper.findPointerIndex(ev, mActivePointerId);
		    final float x = MotionEventWrapper.getX(ev, pointerIndex);
		    final float deltaX = mLastMotionX - x;
		    mLastMotionX = x;

		    if (deltaX < 0) {
			if (mTouchX > 0) {
			    mTouchX += Math.max(-mTouchX, deltaX);
			    mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
			    invalidate();
			}
		    } else if (deltaX > 0) {
			final float availableToScroll = getChildAt(getChildCount() - 1).getRight() - mTouchX - getWidth();
			if (availableToScroll > 0) {
			    mTouchX += Math.min(availableToScroll, deltaX);
			    mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
			    invalidate();
			}
		    } else {
			awakenScrollBars();
		    }
		}
		break;
	    case MotionEvent.ACTION_UP:
		if (mTouchState == TOUCH_STATE_SCROLLING) {
		    final VelocityTracker velocityTracker = mVelocityTracker;
		    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
		    final int velocityX = (int) velocityTracker.getXVelocity();

		    final int screenWidth = getWidth();
		    final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
		    final float scrolledPos = (float) mScrollX / screenWidth;

		    if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
			// Fling hard enough to move left.
			// Don't fling across more than one screen at a time.
			final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen;
			snapToScreen(Math.min(whichScreen, bound), velocityX, true);
		    } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
			// Fling hard enough to move right
			// Don't fling across more than one screen at a time.
			final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen;
			snapToScreen(Math.max(whichScreen, bound), velocityX, true);
		    } else {
			snapToScreen(whichScreen, 0, true);
		    }
		}
		mTouchState = TOUCH_STATE_REST;
		mActivePointerId = INVALID_POINTER;
		releaseVelocityTracker();
		break;
	    case MotionEvent.ACTION_CANCEL:
		if (mTouchState == TOUCH_STATE_SCROLLING) {
		    final int screenWidth = getWidth();
		    final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
		    snapToScreen(whichScreen, 0, true);
		}
		mTouchState = TOUCH_STATE_REST;
		mActivePointerId = INVALID_POINTER;
		releaseVelocityTracker();
		break;
	    case MotionEvent.ACTION_POINTER_UP:
		onSecondaryPointerUp(ev);
		break;
	}

	return true;
    }

    private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
	if (mVelocityTracker == null) {
	    mVelocityTracker = VelocityTracker.obtain();
	}
	mVelocityTracker.addMovement(ev);
    }

    private void releaseVelocityTracker() {
	if (mVelocityTracker != null) {
	    mVelocityTracker.recycle();
	    mVelocityTracker = null;
	}
    }

    void snapToScreen(int whichScreen) {
	snapToScreen(whichScreen, 0, false);
    }

    private void snapToScreen(int whichScreen, int velocity, boolean settle) {
	// if (!mScroller.isFinished()) return;
	int mScrollX = getScrollX();

	whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));

	enableChildrenCache(mCurrentScreen, whichScreen);

	mNextScreen = whichScreen;

	View focusedChild = getFocusedChild();
	if (focusedChild != null && whichScreen != mCurrentScreen && focusedChild == getChildAt(mCurrentScreen)) {
	    focusedChild.clearFocus();
	}

	final int screenDelta = Math.max(1, Math.abs(whichScreen - mCurrentScreen));
	final int newX = whichScreen * getWidth();
	final int delta = newX - mScrollX;
	int duration = (screenDelta + 1) * 100;

	if (!mScroller.isFinished()) {
	    mScroller.abortAnimation();
	}

	if (settle) {
	    mScrollInterpolator.setDistance(screenDelta);
	} else {
	    mScrollInterpolator.disableSettle();
	}

	velocity = Math.abs(velocity);
	if (velocity > 0) {
	    duration += (duration / (velocity / BASELINE_FLING_VELOCITY)) * FLING_VELOCITY_INFLUENCE;
	} else {
	    duration += 100;
	}

	awakenScrollBars(duration);
	mScroller.startScroll(mScrollX, 0, delta, 0, duration);
	invalidate();
    }

    @Override
    protected Parcelable onSaveInstanceState() {
	final SavedState state = new SavedState(super.onSaveInstanceState());
	state.currentScreen = mCurrentScreen;
	return state;
    }

    public boolean awakenScrollBars() {
	try {
	    Method awakenScrollBars = ViewGroup.class.getMethod("awakenScrollBars");
	    return ((Boolean) awakenScrollBars.invoke(this)).booleanValue();
	} catch (NoSuchMethodException e) {
	    // that's fine.
	    return true;
	} catch (IllegalArgumentException e) {
	    throw new RuntimeException(e);
	} catch (IllegalAccessException e) {
	    throw new RuntimeException(e);
	} catch (InvocationTargetException e) {
	    throw new RuntimeException(e);
	}
    }

    public boolean awakenScrollBars(int duration) {
	try {
	    Method awakenScrollBars = View.class.getMethod("awakenScrollBars", int.class);
	    return ((Boolean) awakenScrollBars.invoke(this, duration)).booleanValue();
	} catch (NoSuchMethodException e) {
	    // that's fine.
	    return true;
	} catch (IllegalArgumentException e) {
	    throw new RuntimeException(e);
	} catch (IllegalAccessException e) {
	    throw new RuntimeException(e);
	} catch (InvocationTargetException e) {
	    throw new RuntimeException(e);
	}
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
	SavedState savedState = (SavedState) state;
	super.onRestoreInstanceState(savedState.getSuperState());
	if (savedState.currentScreen != -1) {
	    mCurrentScreen = savedState.currentScreen;
	}
    }

    public void scrollLeft() {
	if (mScroller.isFinished()) {
	    if (mCurrentScreen > 0)
		snapToScreen(mCurrentScreen - 1);
	} else {
	    if (mNextScreen > 0)
		snapToScreen(mNextScreen - 1);
	}
    }

    public void scrollRight() {
	if (mScroller.isFinished()) {
	    if (mCurrentScreen < getChildCount() - 1)
		snapToScreen(mCurrentScreen + 1);
	} else {
	    if (mNextScreen < getChildCount() - 1)
		snapToScreen(mNextScreen + 1);
	}
    }

    public int getScreenForView(View v) {
	int result = -1;
	if (v != null) {
	    ViewParent vp = v.getParent();
	    int count = getChildCount();
	    for (int i = 0; i < count; i++) {
		if (vp == getChildAt(i)) {
		    return i;
		}
	    }
	}
	return result;
    }

    /**
     * @return True is long presses are still allowed for the current touch
     */
    public boolean allowLongPress() {
	return mAllowLongPress;
    }

    /**
     * Set true to allow long-press events to be triggered, usually checked by
     * {@link Launcher} to accept or block dpad-initiated long-presses.
     */
    public void setAllowLongPress(boolean allowLongPress) {
	mAllowLongPress = allowLongPress;
    }

    public void moveToDefaultScreen() {
	moveToDefaultScreen(true);
    }

    void moveToDefaultScreen(boolean animate) {
	setCurrentScreen(mDefaultScreen, animate);
	getChildAt(mDefaultScreen).requestFocus();
    }

    private void notifyListenersChanged() {
	for (OnPageListener listener : mListeners) {
	    if (listener != null) {
		listener.onPageChanged(this, mCurrentScreen);
	    }
	}
    }

    private void notifyListenersScroll(int scrollX) {
	for (OnPageListener listener : mListeners) {
	    if (listener != null) {
		listener.onScroll(this, scrollX);
	    }
	}
    }

    public static class SavedState extends BaseSavedState {
	int currentScreen = -1;

	SavedState(Parcelable superState) {
	    super(superState);
	}

	private SavedState(Parcel in) {
	    super(in);
	    currentScreen = in.readInt();
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
	    super.writeToParcel(out, flags);
	    out.writeInt(currentScreen);
	}

	public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
	    public SavedState createFromParcel(Parcel in) {
		return new SavedState(in);
	    }

	    public SavedState[] newArray(int size) {
		return new SavedState[size];
	    }
	};
    }

    private static class MotionEventWrapper {

	public static final String TAG = MotionEventWrapper.class.getSimpleName();
	private static Method getX, getY, getPointerId, findPointerIndex;

	private static final boolean IS_OLD = Build.VERSION.SDK_INT <= Build.VERSION_CODES.DONUT;

	static {
	    initCompatibility();
	}

	private static void initCompatibility() {
	    if (!IS_OLD) {
		try {
		    getX = MotionEvent.class.getMethod("getX", int.class);
		    getY = MotionEvent.class.getMethod("getY", int.class);
		    getPointerId = MotionEvent.class.getMethod("getPointerId", int.class);
		    findPointerIndex = MotionEvent.class.getMethod("findPointerIndex", int.class);
		} catch (Throwable e) {
		    throw new RuntimeException(e);
		}
	    }
	}

	public static float getX(MotionEvent ev, int optPointerIndex) {
	    if (IS_OLD) {
		return ev.getX();
	    } else {
		try {
		    return ((Float) getX.invoke(ev, optPointerIndex)).floatValue();
		} catch (Throwable e) {
		    throw new RuntimeException(e);
		}
	    }
	}

	public static float getY(MotionEvent ev, int optPointerIndex) {
	    if (IS_OLD) {
		return ev.getY();
	    } else {
		try {
		    return ((Float) getY.invoke(ev, optPointerIndex)).floatValue();
		} catch (Throwable e) {
		    throw new RuntimeException(e);
		}
	    }
	}

	public static int getPointerId(MotionEvent ev, int optPointerIndex) {
	    if (IS_OLD) {
		return 0;
	    } else {
		try {
		    return ((Integer) getPointerId.invoke(ev, optPointerIndex)).intValue();
		} catch (Throwable e) {
		    throw new RuntimeException(e);
		}
	    }
	}

	public static int findPointerIndex(MotionEvent ev, int activePointerId) {
	    if (IS_OLD) {
		return 0;
	    } else {
		try {
		    return ((Integer) findPointerIndex.invoke(ev, activePointerId)).intValue();
		} catch (Throwable e) {
		    throw new RuntimeException(e);
		}
	    }
	}

    }

}
